﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QLTV.Forms.KeHoach
{
    public partial class frmRankCourse : DevExpress.XtraEditors.XtraForm
    {
        public frmRankCourse()
        {
            InitializeComponent();
        }

        public int TrainingPlanId
        {
            get;
            set;
        }

        private void frmRankCourse_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsTraining.tblCrewType' table. You can move, or remove it, as needed.
            this.tblCrewTypeTableAdapter.Fill(this.dsTraining.tblCrewType);
            // TODO: This line of code loads data into the 'dsTraining.tblTrainingCourse' table. You can move, or remove it, as needed.
            this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);
            // TODO: This line of code loads data into the 'dsTraining.tblRank' table. You can move, or remove it, as needed.
            this.tblRankTableAdapter.Fill(this.dsTraining.tblRank);
        }

        private void RankGridLookUp_EditValueChanged(object sender, EventArgs e)
        {
            if (RankGridLookUp.EditValue != null)
            {
                for (int i = 0; i < cklCourse.ItemCount; i++)
                {
                    cklCourse.SetItemChecked(i, false);
                }

                using (QLTV.Database.dsTrainingTableAdapters.tblRankCourseTableAdapter adapter = new Database.dsTrainingTableAdapters.tblRankCourseTableAdapter())
                {
                    QLTV.Database.dsTraining.tblRankCourseDataTable table = adapter.GetDataByRankId(int.Parse(RankGridLookUp.EditValue.ToString()));
                    if (table.Rows.Count > 0)
                    {
                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            QLTV.Database.dsTraining.tblRankCourseRow row = table[i];
                            for (int j = 0; j < cklCourse.ItemCount; j++)
                            {
                                if (row.nTraningCourse == ((QLTV.Database.dsTraining.tblTrainingCourseRow)((System.Data.DataRowView)cklCourse.GetItem(j)).Row).nTrainingCourse)
                                {
                                    cklCourse.SetItemChecked(j, true);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (RankGridLookUp.EditValue != null)
            {
                using (QLTV.Database.dsTrainingTableAdapters.tblRankCourseTableAdapter trainingAdapter = new Database.dsTrainingTableAdapters.tblRankCourseTableAdapter())
                {
                    QLTV.Database.dsTraining.tblRankCourseDataTable trainingTable = trainingAdapter.GetDataByRankId(int.Parse(RankGridLookUp.EditValue.ToString()));
                    if (trainingTable.Rows.Count > 0)
                    {
                        for (int i = 0; i < trainingTable.Rows.Count; i++)
                        {
                            QLTV.Database.dsTraining.tblRankCourseRow RankCourseRow = trainingTable[i];
                            RankCourseRow.Delete();
                        }
                        trainingAdapter.Update(trainingTable);
                    }

                    using (QLTV.Database.dsTraining.tblRankCourseDataTable newTrainingTable = new Database.dsTraining.tblRankCourseDataTable())
                    {
                        foreach (System.Data.DataRowView item in cklCourse.CheckedItems)
                        {
                            QLTV.Database.dsTraining.tblRankCourseRow newRankCourseRow = newTrainingTable.NewtblRankCourseRow();

                            newRankCourseRow.nTraningCourse = ((QLTV.Database.dsTraining.tblTrainingCourseRow)item.Row).nTrainingCourse;
                            newRankCourseRow.nRankId = int.Parse(RankGridLookUp.EditValue.ToString());

                            newTrainingTable.AddtblRankCourseRow(newRankCourseRow);
                        }
                        trainingAdapter.Update(newTrainingTable);
                    }

                }
            }
            this.Close();
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            this.Close();
           
        }
    }
}